package com.shen.system.controller;

import com.shen.system.entity.UserEntity;
import com.shen.system.info.UserInfo;
import com.shen.system.service.UserService;
import com.shen.api.code.SystemCode;
import com.shen.api.commons.ResponseResult;
import com.shen.api.commons.ResponseResultFactory;
import com.shen.api.commons.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    final Logger logger = LoggerFactory.getLogger(UserEntity.class);

    final long UPD_USER_ZERO = 0 ;  //ID 为0
    final int NAME_LENGTH= 3;
    final int ACCOUNT_LENGTH = 6;
    final int PASS_LENGTH = 6;
    @Autowired
    UserService userService;

    @RequestMapping("/addUser")
    public ResponseResult addUser(UserEntity userEntity){
        //全部为空
        if (SystemUtils.isNull(userEntity)){
            logger.error("system user addUser userEntity is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_PARAM_NULL);
            logger.info("system user addUser return msg : "+ responseResult);
            return responseResult;
        }
        //用户名为空
        if (SystemUtils.isNullOr(userEntity.getUname())){
            logger.error("system user addUser uname is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_NAME_NULL);
            logger.info("system user addUser return msg : "+ responseResult);
            return responseResult;
        }
        //账号为空
        if (SystemUtils.isNullOr(userEntity.getUaccount())){
            logger.error("system user addUser uaccount is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_ACCOUNT_NULL);
            logger.info("system user addUser return msg : "+ responseResult);
            return responseResult;
        }
        //密码为空
        if (SystemUtils.isNullOr(userEntity.getUpass())){
            logger.error("system user addUser upass is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_PASS_NULL);
            logger.info("system user addUser return msg : "+ responseResult);
            return responseResult;
        }
        //手机号为空
        if (SystemUtils.isNullOr(userEntity.getUphone())){
            logger.error("system user addUser uphone is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_PHONE_NULL);
            logger.info("system user addUser return msg : "+ responseResult);
            return responseResult;
        }
        //用户名小于3个长度
        if (userEntity.getUname().trim().length()>NAME_LENGTH){
            logger.error("system user addUser uname is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_NAME_SIZE);
            logger.info("system user addUser return msg : "+ responseResult);
            return responseResult;
        }
        //账号小于6个长度
        if (userEntity.getUaccount().trim().length()>ACCOUNT_LENGTH){
            logger.error("system user addUser uaccount is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_ACCOUNT_SIZE);
            return responseResult;
        }
        //密码少于6个长度
        if (userEntity.getUpass().trim().length()>PASS_LENGTH){
            logger.error("system user addUser upass is null");
            logger.info("param : "+ userEntity);
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_PASS_SIZE);
            logger.info("system user addUser return msg : "+ responseResult);
            return responseResult;
        }

        //在这里把密码记录成文明

        //密码的加密操作  md5 加密
        logger.info("system user addUser pass digest");
        userEntity.setUpass(DigestUtils.md5DigestAsHex(userEntity.getUpass().getBytes()));
        //保存用户信息
        logger.info("system user addUser pass digest");
        boolean result = userService.addUser(userEntity);
        logger.info("system user addUser userService addUser end : "+ result);
        //根据保存的结果返回给前台信息
        ResponseResult responseResult = returnResponseResult(result, SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_ACCOUNT_SIZE);
        logger.info("system user addUser return msg : "+ responseResult);
        return responseResult;
    }

    /**
     * 删除功能
     * @param uid = 1就是删除一个 1,2,3就是多个删除
     * @return
     */
    @PostMapping("/delUser")
    public ResponseResult delUser( String uid){
        logger.info("system user delUser start");
        //传过来的参数是空的
        if (SystemUtils.isNullOr(uid)) {
            logger.error("system user delUser uid is null");
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_DEL_FAIL_UID_NULL);
            logger.info("system user delUser return msg : " + responseResult);
        }
        //简单的逻辑判断 ；  1、可以在这判断 2、交给业务层
        logger.info("system user delUser UserService start");
        boolean b = userService.delUser(uid);
        logger.info("system user delUser UserService end : "+ b);

        ResponseResult responseResult;
        if (b){
            logger.info("system user delUser success");
            responseResult = new ResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS);
        }else {
            logger.info("system user delUser error");
            responseResult = new ResponseResult(SystemCode.TRAFFIC_SYSTEM_ERROR);
        }
        return responseResult;

    }

    @PostMapping("/updUser")
    public ResponseResult updUser(UserEntity userEntity){

        if (SystemUtils.isNull(userEntity)){
            logger.error("system user updUser UserEntity is null");
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_UPD_FAIL_PARAM_NULL);
            logger.info("system user updUser return msg : "+ responseResult);
            return responseResult;
        }
        if (SystemUtils.isNull(userEntity.getUid()) || userEntity.getUid() == UPD_USER_ZERO){
            logger.error("system user updUser UserEntity is null");
            ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.SYSTEM_USER_ERROR_UPD_FAIL_UID_NULL);
            logger.info("system user updUser return msg : "+ responseResult);
            return responseResult;
        }
        boolean b = userService.updUser(userEntity);
        return returnResponseResult(b,SystemCode.SYSTEM_USER_ERROR_UPD_FAIL);
    }

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping("/allUser")
    public ResponseResult<List<UserEntity>> selectUser( ){

        List<UserEntity> b = userService.findAllUser();
        ResponseResult<List<UserEntity>> responseResult =
                ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS,b);
        return responseResult;
    }

    /**
     * 条件查询
     * @param userEntity
     * @return
     */
    @GetMapping("/findBy")
    public ResponseResult<List<UserEntity>> finAdd(UserEntity userEntity){

        List<UserEntity> user =  userService.findaddAllUserByWhere(userEntity);
        ResponseResult responseResult=ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS,user);
        return responseResult;

    }

    /**
     * 根据时间查询  开始-结束
     * @param userEntity
     * @return
     */
    @GetMapping("/findUsersByTime")
    public ResponseResult<List<UserEntity>> finyUsersByTime(UserEntity userEntity){
        List<UserEntity> user =  userService.findAllByUtimeBetween(userEntity.getStartTime(),userEntity.getEndTime());
        ResponseResult responseResult=ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS,user);
        return responseResult;
    }


    /**
     * 分页查询+条件查询
     * @param userEntity
     * @return
     */
    @GetMapping("/queryUsers")
    public ResponseResult queryUsers(UserEntity userEntity){
        //分页查询+条件查询
        Map<String, Object> map = userService.queryUsers(userEntity);
        ResponseResult responseResult = ResponseResultFactory.buildResponseResult(SystemCode.TRAFFIC_SYSTEM_SUCCESS,map);

        return responseResult;
    }

    private ResponseResult returnResponseResult(boolean b, String code) {
        ResponseResult responseResult;
        if (b) {
            responseResult = ResponseResultFactory.buildResponseResult();
        } else {
            responseResult = ResponseResultFactory.buildResponseResult(code);
            logger.error("system user returnResponseResult fail : "+ code);
        }
        logger.info("system user returnResponseResult ,return :"+responseResult);
        return responseResult;
    }
}
